<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./js/vue.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <li v-for="(item,index) in arr">
                <h3>名称：{{item.name}}</h3>
                <p>单价：{{item.price}}</p>
                <button @click="add(index)">-</button>
                <span>{{item.num}}</span>
                <button @click="del(index)">+</button>
                <p>价格：{{item.price*item.num}}</p>
            </li>
        </ul>
        <p>总价{{allprice}}</p>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                arr: [{
                        "name": "苹果",
                        "price": 20,
                        "num": 0
                    },
                    {
                        "name": "桃子",
                        "price": 10,
                        "num": 0
                    },
                    {
                        "name": "西瓜",
                        "price": 30,
                        "num": 0
                    },
                ],
                allprice:0

            },
            methods: {
                add(index){
                    if(this.arr[index].num==0)return;
                    this.arr[index].num--;
                    this.sum();
                },
                del(index){
                    this.arr[index].num++;
                    this.sum();

                },
                sum(){
                    var allprice=0;
                    this.arr.forEach((item,index)=>{
                        allprice+=item.num*item.price
                    })
                    this.allprice=allprice;
                }
            }
        })
    </script>

</body>

</html>